package cn.carl.frame.concurrent.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import cn.carl.frame.concurrent.Dual;
import cn.carl.frame.concurrent.ThreadHelper;

/**
 * 多线程服务类
 * 
 * @author Carl
 *
 */
public class DualImpl implements Dual {

	@Override
	public void execute(List<Runnable> runnables) {
		Objects.requireNonNull(runnables);
		// 执行任务
		runnables.forEach(ThreadHelper::exe);
	}

	@Override
	public List<Future<?>> submit(List<Callable<?>> callables) {
		Objects.requireNonNull(callables);
		// 并行提交任务
		return callables.parallelStream().collect(ArrayList::new, (cls, a) -> cls.add(ThreadHelper.sub(a)),
				ArrayList::addAll);
	}

}